package com.wxl.xueyuan.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wxl.xueyuan.MyApp;
import com.wxl.xueyuan.R;
import com.wxl.xueyuan.adapter.LoginfoAdapter;
import com.wxl.xueyuan.api.Api;
import com.wxl.xueyuan.api.ApiConfig;
import com.wxl.xueyuan.api.MCallback;
import com.wxl.xueyuan.entity.LoginfoEntity;
import com.wxl.xueyuan.entity.LoginfoListResponse;
import com.wxl.xueyuan.util.ActivityCollectorUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import lombok.SneakyThrows;

public class LoginfoActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private RefreshLayout refreshLayout;
    private int pageNum = 1;
    private LoginfoAdapter loginfoAdapter;
    private List<LoginfoEntity> datas = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    loginfoAdapter.setDatas(datas);
                    loginfoAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_loginfo;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        ActivityCollectorUtil.addActivity(LoginfoActivity.this);
    }

    @Override
    protected void initData() {
        ImageButton back = findViewById(R.id.back);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        loginfoAdapter = new LoginfoAdapter(this);
        recyclerView.setAdapter(loginfoAdapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                getLoginfoList(true);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pageNum++;
                getLoginfoList(false);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginfoActivity.super.onBackPressed();
            }
        });
        getLoginfoList(true);
    }

    private void getLoginfoList(final boolean isRefresh) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("ID", MyApp.getmData("ID"));
        params.put("page", pageNum);
        params.put("size", ApiConfig.PAGE_SIZE);
        Api.config("/user/queryAllLoginInfoPS", params).getRequest(this, new MCallback() {
            @SneakyThrows
            @Override
            public void onSuccess(final String res) {
                if (isRefresh) {
                    refreshLayout.finishRefresh(true);
                } else {
                    refreshLayout.finishLoadMore(true);
                }
                ObjectMapper mapper = new ObjectMapper();
                LoginfoListResponse response = mapper.readValue(res, LoginfoListResponse.class);
                if (response != null) {
                    List<LoginfoEntity> list = response.getItems();
                    if (list != null && list.size() > 0) {
                        if (isRefresh) {
                            datas = list;
                        } else {
                            datas.addAll(list);
                        }
                        mHandler.sendEmptyMessage(0);
                    } else {
                        if (isRefresh) {
                            showToastSync("暂时无数据");
                        } else {
                            showToastSync("没有更多数据");
                        }
                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
                if (isRefresh) {
                    refreshLayout.finishRefresh(true);
                } else {
                    refreshLayout.finishLoadMore(true);
                }
            }
        });
    }
}